#include <QMetaObject>
#include <QMetaProperty>

#include "arsxmladapter.h"

////////// ARSXMLAdapter ///////////////////////////////////////////////////////////////////////////

StaticModelFileAdapter::StaticModelFileAdapter(QObject * parent)
    : QObject(parent)
{

}

QString StaticModelFileAdapter::name() const
{
    return "arsxmladapter";
}

QString StaticModelFileAdapter::description() const
{
    return tr("This plugin provides storing ARS objects to XML.");
}

bool StaticModelFileAdapter::loadSettings()
{
    return 0;
}

ServerModel * StaticModelFileAdapter::createModel(QIODevice * device)
{
    QDomDocument document;
    if(!document.setContent(device))
        return 0;

    ServerModel * model = new ServerModel();

    QDomElement element = document.firstChildElement();
    element = element.firstChildElement();
    while(!element.isNull())
    {
        ARSWorkflowObject * object = parseObject(model, element);
        if(object)
            model->addWorkflowObject(object);

        element = element.nextSiblingElement();
    }

    return model;
}

bool StaticModelFileAdapter::saveModel(ServerModel * model, QIODevice * dest)
{
    if(!dest->isWritable())
        return false;

    ARSWorkflowObjectList list = model->workflowList();

    foreach(ARSWorkflowObject * object, list)
    {
        const QMetaObject * metaObject = object->metaObject();
        for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++)
        {
            const char * propertyName = metaObject->property(i).name();
            QString propertyString = value2string(object->property(propertyName));
            dest->write(QString(QString::fromAscii(propertyName) + " " + propertyString + "\n").toAscii());
        }

    }

    return true;
}

ARSWorkflowObject * StaticModelFileAdapter::parseObject(ServerModel * model, const QDomElement & element)
{
    ARSWorkflowObject * object = 0;
    QString namePropertyTag;

    if(element.tagName() == "activeLink")
    {
        object = new ARSActiveLink(model);
        namePropertyTag = "activeLinkName";
    }
    else
        if(element.tagName() == "filter")
        {
            object = new ARSFilter(model);
            namePropertyTag = "filterName";
        }
        else
            return 0;

    QDomElement propertyElement = element.firstChildElement(namePropertyTag);
    object->setName(propertyElement.text());
    propertyElement = element.firstChildElement("modifiedDate");
    object->setModifiedDate(QDateTime::fromString(propertyElement.text(), Qt::ISODate));
    propertyElement = element.firstChildElement("changeHistory");
    QDomElement changeHistoryElement = propertyElement.firstChildElement("changeEntry");
    QStringList changeHistory;
    while(!changeHistoryElement.isNull())
    {
        QString entryString = changeHistoryElement.attribute("modifiedDate") + "\n";
        QDomElement changeHistoryEntryElement = changeHistoryElement.firstChildElement("entry");
        if(!changeHistoryEntryElement.isNull())
            changeHistory += entryString + changeHistoryEntryElement.text();

        changeHistoryElement = changeHistoryElement.nextSiblingElement();
    }

    object->setChangeHistory(changeHistory);

    return object;
}

QString StaticModelFileAdapter::value2string(const QVariant & value)
{
    if(value.canConvert(QVariant::String))
        return value.toString();

    return QString();
}

////////////////////////////////////////////////////////////////////////////////////////////////////

//Q_EXPORT_PLUGIN2(arsxmladapterplugin, ARSXMLAdapter)

////////////////////////////////////////////////////////////////////////////////////////////////////

